package com.dream21th.generate.biz;

import com.dream21th.common.enums.RtnEnum;
import com.dream21th.common.exception.WebException;
import com.dream21th.common.utils.ZipUtil;
import com.dream21th.generate.dto.MysqlDTO;
import com.dream21th.generate.dto.TableDTO;
import com.dream21th.generate.util.DBUtils;
import com.dream21th.generate.util.FreeMakeUtils;
import com.dream21th.generate.util.GenerateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @Auther: hp
 * @Date: 2019/11/25 14:57
 * @Description:
 */
@Slf4j
@Component
public class GenerateBiz {

    private final static List<String> SYSTEM_DATABASE= Arrays.asList("information_schema","mysql","performance_schema","sys");

    /**
     * 查询mysql中的所有数据库（去掉系统库）
     * @param dto
     * @return
     */
    public List<String> obtainlDataBases(MysqlDTO dto)  {
        List<String> lists = new ArrayList();
        try{
            DBUtils dbUtils = new DBUtils();
            String sql="select DISTINCT(table_schema) from information_schema.tables";
            dbUtils.queryTableInfo(dto,sql);
            ResultSet rs = dbUtils.getRs();
            while (rs.next()) {
                lists.add(rs.getString("table_schema"));
            }
            dbUtils.closed();
        }catch (Exception exception){
            log.error("发生错误{}",exception);
            throw new WebException(RtnEnum.ERRROR);
        }
        lists.removeAll(SYSTEM_DATABASE);
        return lists;
    }

    /**
     * 根据数据库获取所有表
     * @param dto
     * @return
     */
    public List<TableDTO> obtainTables(MysqlDTO dto){
        List<TableDTO> lists = new ArrayList();
        try{
            DBUtils dbUtils = new DBUtils();
            String sql=String.format("select * from information_schema.tables where table_schema='%s'",dto.getDataBase());
            dbUtils.queryTableInfo(dto,sql);
            ResultSet rs = dbUtils.getRs();
            while (rs.next()) {
                String tableName = rs.getString("TABLE_NAME");
                String tableComment = rs.getString("TABLE_COMMENT");
                lists.add(TableDTO.builder().tableName(tableName).tableComment(tableComment).build());
            }
            dbUtils.closed();
        }catch (Exception exception){
            log.error("发生错误{}",exception);
            throw new WebException(RtnEnum.ERRROR);
        }
        return lists;
    }
    /**
     * 生成代码并打成ZIP包给前端下载
     * @param dto
     */
    public String generateCode(MysqlDTO dto) {
        try{
            File file = new File(FreeMakeUtils.CODE_PATH_TMP_CREATE);
            file.delete();
            GenerateCodeUtils.generateCode(dto);
            ZipUtil.toZip(FreeMakeUtils.CODE_PATH_TMP_CREATE,FreeMakeUtils.CODE_PATH_TMP_ZIP+"/code.zip",true);
            return "/api/generate/"+FreeMakeUtils.CODE_PATH_TMP_ZIP+"/code.zip";
        }catch (Exception exception){
            log.error("异常错误：{}",exception);
            throw new WebException(RtnEnum.GENERAT_CODE_ERROR);
        }
    }
}
